package com.legend.ds.config;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;


/**
 * @Auther: tangwei
 * @Date: 2021/7/12 下午2:39
 * @Description: 类描述信息
 */
@Slf4j
public class LanzhenDynamicDataSourceContextHolder {


    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();//当前线程

    public static List<String> dataSourceIds = new ArrayList<String>(); //保存系统内已经初始化好的dataSource的key

    public static void setDataSourceType(String dataSourceType){//当前线程使用哪个dataSource的key

        log.info("切换到数据源："+dataSourceType);
        contextHolder.set(dataSourceType);

    }

    public static String getDataSourceType(){
        return contextHolder.get();
    }

    /**
     * 设置数据源之前一定要先移除
     */
    public static void removeDataSourceRouterKey () {
        contextHolder.remove();
    }

    public static boolean containsDatasource(String dataSourceId){
        return dataSourceIds.contains(dataSourceId);
    }
}


